package com.cby.jetpack.sample.paging3.holder

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.paging.LoadState
import androidx.recyclerview.widget.RecyclerView
import com.cby.jetpack.sample.R
import com.cby.jetpack.sample.databinding.PagingLoadMoreBinding

/**
 * @description: LoadStateViewHolder
 *
 * @author ChenBaoyang
 * @date 2021/8/13 11:33
 */
class LoadStateViewHolder(parent: ViewGroup, var retry: () -> Unit) : RecyclerView.ViewHolder(
    LayoutInflater.from(parent.context).inflate(R.layout.paging_load_more, parent, false)
) {

    val binding: PagingLoadMoreBinding = PagingLoadMoreBinding.bind(itemView)

    fun bindState(loadState: LoadState) {
        if(loadState is LoadState.Error) {
            binding.btnRetry.visibility = View.VISIBLE
            binding.btnRetry.setOnClickListener {
                retry()
            }
        }else if(loadState is LoadState.Loading){
            binding.llLoading.visibility = View.VISIBLE
        }
    }
}